package com.mytime.feeds;

import org.restlet.resource.ServerResource;

public class Feeder extends ServerResource {
	
/*	private static ArrayList<String> cities = new ArrayList<String>();
	
    *//**
     * Handles a GET HTTP request.
     * Returns all events from a given category. 
     * @param entity
     *//*
	@Get
	public Representation getData() throws Exception {
		JSONObject result = new JSONObject();
		
		cities.add("Rio de Janeiro");
		
		//Add categories
		addCategories(result);
		
		//Add venues
		addVenues(result);
		
		//Add events
		addEvents(result);	
		return getRepresentation(result);
	}
	
	private void addCategories(JSONObject result) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		//Get categories from eventful API
		JSONObject categoriesObj = CategoriesFeed.getData();
		
		try {
			JSONArray categories = categoriesObj.getJSONArray("categories");
			ArrayList<Category> categoriesList = new ArrayList<Category>();
			int categoriesCount = 0;

			//Iterate over categories
			for (int i = 0; i < categories.length(); i++) {
				JSONObject category = categories.getJSONObject(i);
				String id = Functions.removeSpecialCharacters(category.getString("id")).replaceAll(" ", "_").toLowerCase();
				String name = category.getString("name");
								
				Category c = new Category(id, name);
				categoriesList.add(c);
				categoriesCount += 1;
			}
			
			//save all categories
			pm.makePersistentAll(categoriesList);
			
			result.put("categories", categoriesCount);
			
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	private void addVenues(JSONObject result) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		//TODO: add for to get more than one city
		
		//Get categories from eventful API
		JSONObject venuesObj = VenuesFeed.getData("Rio de Janeiro", "shopping", 30, null);
		
		try {
			JSONArray venues = venuesObj.getJSONArray("venues");
			ArrayList<Venue> venuesList = new ArrayList<Venue>();
			int venuesCount = 0;

			//Iterate over categories
			for (int i = 0; i < venues.length(); i++) {
				JSONObject venue = venues.getJSONObject(i);
				String id = Functions.removeSpecialCharacters(venue.getString("name"))
						.replaceAll(" ", "_").toLowerCase();
																
				Venue v = new Venue();
				v.setKey(id);
				v.setName(venue.getString("name"));
				v.setDescription(venue.getString("description"));
				v.setAddress(venue.getString("address"));
				v.setCityId(venue.getString("city"));
				v.setRegion(venue.getString("region"));
				v.setPostalCode(venue.getString("postalCode"));
				v.setCountry(venue.getString("country"));
				v.setLatitude(venue.getString("latitude"));
				v.setLongitude(venue.getString("longitude"));

				venuesList.add(v);
				venuesCount += 1;
			}
			
			//save all categories
			pm.makePersistentAll(venuesList);
			
			result.put("venues", venuesCount);
			
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}
 
	private void addEvents(JSONObject result) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		//Get events from eventful API
		JSONObject eventsObj = EventsFeed.getData("Rio de Janeiro", "festivals_parades");
		
		try {
			JSONArray eventsList = eventsObj.getJSONArray("events");
			
			for (int i = 0; i < eventsList.length(); i++) {
				System.out.println(eventsList.get(i));
			}
			
			//System.out.println(eventsList);
			//System.out.println(venuesList);
			JSONArray events = eventsObj.getJSONArray("events");
			ArrayList<Event> eventsList = new ArrayList<Event>();
			int eventsCount = 0;
			
			//Iterate over events
			for (int i = 0; i < events.length(); i++) {
				
				Event e = new Event();
				e.setId();
				e.setName();
				e.setStartTime();
				e.setEndTime();
				
				eventsList.add(e);
				eventsCount += 1;
			}
			
			//save all events
			pm.makePersistentAll(eventsList);
			
			result.put("categories", eventsCount);
			
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
	}

	*//**
	 * Returns the JSON representation for a given JSON object.
	 * *//*
    private Representation getRepresentation(JSONObject jsonObject) {
    	JsonRepresentation jr = new JsonRepresentation(jsonObject);
    	jr.setCharacterSet(CharacterSet.UTF_8);
    	return jr;
    }    

*/}
